package com.rrx.mapper;

import com.rrx.model.Administrator;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;

@Mapper
public interface AdministratorMapper {

    /**
     * 查询所有管理员
     * @return 管理员数组
     */
    @Select("select * from admin")
    public Administrator[] getAllAdmins();

    /**
     * 通过登录查询管理员
     * @param username 用户名
     * @param password 密码
     * @return 查询到的管理员
     */
    @Select("select * from admin where username=#{username} and password=#{password}")
    public Administrator getAdminByLogin(@Param("username") String username, @Param("password") String password);

    /**
     * 通过用户名查询管理员是否存在
     * @param username 用户名
     * @return 存在管理员的用户名
     */
    @Select("select count(username) from admin where username=#{username}")
    public int isAdminExists(@Param("username") String username);

    /**
     * 添加管理员
     * @param username 用户名
     * @param password  密码
     * @return 添加是否成功
     */
    @Insert("insert into admin (username, password, admintime,email) values(#{username}, #{password}, now(), #{email})")
    public int addAdmin(@Param("username") String username, @Param("password") String password, @Param("email") String email);

    /**
     * 修改管理员密码
     * @param password 新密码
     * @param id       管理员ID
     * @return
     */
    @Update("update admin set password = #{password} where id = #{id}")
    public int updatePassword(@Param("password") String password, @Param("id") int id);

    /**
     * 修改管理员状态
     * @param state   修改后状态
     * @param id      管理员ID
     * @return
     */
    @Update("update admin set state = #{state} where id = #{id}")
    public int updateState(@Param("state") int state, @Param("id") int id);

    /**
     * 修改管理员信息
     * @param password   密码
     * @param email      邮箱
     * @param state      状态
     * @return
     */
    @Update("update admin set username = #{username}, password = #{password}, email = #{email}, state = #{state} where id = #{id}")
    public int updateAdmin(@Param("username") String username, @Param("password") String password, @Param("email") String email, @Param("state") int state, @Param("id") int id);

    @Select("select * from admin order by id asc limit #{limit}, 10")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "password", property = "password"),
            @Result(column = "admintime", property = "adminTime"),
            @Result(column = "email", property = "email"),
            @Result(column = "state", property = "state")
    })
    public Administrator[] getAdminsLimited(@Param("limit") int limit);

    @Select("select count(id) from admin")
    public int getAdminCount();

    /**
     * 通过用户名查询管理员ID
     * @param username   用户名
     * @return
     */
    @Select("select id from admin where username = #{username}")
    public Integer getIdByUsername(@Param("username") String username);

    @Delete("delete from admin_and_role where adminid = #{adminId}")
    public int deleteAdminRoles(@Param("adminId") int adminId);
}
